package com.xxx.modules.controller;;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xxx.modules.entity.User;
import com.xxx.modules.mapper.UserMapper;
import com.xxx.modules.service.ArchivesService;
import com.xxx.modules.entity.Archives;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import com.github.pagehelper.PageInfo;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.xxx.modules.utils.Result;
import com.xxx.modules.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * archives
 *
 * @author 北易航
 * @since 1.0.0 2022-12-28
 */
@RestController
@RequestMapping("/common/archives")
@Api(tags="archives")
public class ArchivesController {
    @Autowired
    private ArchivesService archivesService;

    /**
    *  获取所有老人档案管理
    * @param pageNum
    * @param pageSize
    * @param archives
    * @return
    */

    @GetMapping("/getArchivesInfo")
    @ApiOperation("获取所有档案管理")
    public Result getArchivesInfo(Archives archives, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                      @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<Archives> data = archivesService.selectArchivesInfo(archives,pageNum,pageSize);
        return ResultUtil.success(1,"成功",data);
    }

    /**
    * 保存老人档案管理
    * @param archives
    * @return
    */
    @PostMapping("/saveArchivesInfo")
    @ApiOperation("保存档案管理")
    public Result saveArchivesInfo(@RequestBody Archives archives){
        archivesService.saveArchivesInfo(archives);
        return ResultUtil.success(1,"成功",null);
    }


    /**
     * 更新老人档案管理
     * @param archives
     * @return
     */

    @PutMapping("/updateArchivesInfo")
    @ApiOperation("更新档案管理")
    public Result updateArchivesInfo(@RequestBody Archives archives){
        archivesService.updateArchivesInfo(archives);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id删除老人档案管理
     * @param id
     * @return
     */
    @DeleteMapping("/delArchivesInfo")
    @ApiOperation("根据id删除档案管理")
    public Result delArchivesInfo(Integer id){
        archivesService.delArchivesInfo(id);
        return ResultUtil.success(1,"成功",null);
    }

    /**
     * 根据id集合批量删除老人档案管理
     * @param idList
     * @return
     */
    @DeleteMapping("/delBatchArchivesInfo")
    @ApiOperation("根据id集合批量删除档案管理")
    public Result delBatchArchivesInfo(String idList){
        archivesService.delBatchArchivesInfo(idList);
        return ResultUtil.success(1,"成功",null);
    }

    @Autowired
    private UserMapper userMapper;

    @PutMapping("/bindWork")
    public Result bindWork(@RequestBody User user){
        try {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("work_id",user.getWorkId());
//            User user1 = userMapper.selectOne(wrapper);
//            if (user1 !=null){
//              return ResultUtil.error(-1,"该护工已经绑定其他用户了");
//            }
            userMapper.updateById(user);
            return ResultUtil.success(1,"正常",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"异常");
        }
    }

    /**
     *  绑定护工
     * @param user
     * @return
     */
    @PutMapping("/binding")
    public Result binding(@RequestBody User user){
        try {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("binding_id",user.getBindingId());

            archivesService.update(null,new LambdaUpdateWrapper<Archives>()
                    .set(Archives::getBindingId,user.getBindingId())
                    .eq(Archives::getId,user.getId())) ;
            //userMapper.updateById(user);
            return ResultUtil.success(1,"正常",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"异常");
        }
    }

    @GetMapping("/getOldPeopleInfo")
    @ApiOperation("获取老人信息")
    public Result getOldPeopleInfo(Archives archives, @RequestParam(value="pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value="pageSize",defaultValue = "10") Integer pageSize){
        PageInfo<Archives> data = archivesService.selectOldPeopleInfo(archives,pageNum,pageSize);
        return ResultUtil.success(1,"成功",data);
    }
}